<?php

/**
 * This is the model class for table "jobs".
 *
 * The followings are the available columns in table 'jobs':
 * @property integer $id
 * @property string $for_model
 * @property integer $foreign_key
 * @property string $type
 * @property integer $handled_date
 * @property string $handled_by
 * @property integer $complete_date
 */
class jobs extends ActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return jobs the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'jobs';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('for_model, foreign_key, type, handled_date', 'required'),
            array('foreign_key, handled_date, complete_date, document_id', 'numerical', 'integerOnly' => true),
            array('for_model, type', 'length', 'max' => 125),
            array('handled_by', 'length', 'max' => 1024),
            array('signed_by', 'length', 'max' => 50),
            array('document_id', 'exist', 'className' => 'document', 'attributeName' => 'id'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, for_model, foreign_key, type, handled_date, handled_by, complete_date', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'for_model' => 'For Model',
            'foreign_key' => 'Foreign Key',
            'type' => 'Type',
            'handled_date' => 'Handled Date',
            'handled_by' => 'Handled By',
            'complete_date' => 'Complete Date',
        );
    }

    public function AddJob($model, $type) {
        $this->for_model = $model->tableName();
        $this->foreign_key = $model->getPrimaryKey();
        $this->handled_date = time();
        //$this->handled_by = Yii::app()->user->getState('user');
        $this->type = $type;
        return parent::save();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('for_model', $this->for_model);
        $criteria->compare('foreign_key',$this->foreign_key);
        $criteria->compare('handled_date', $this->handled_date);
        $criteria->compare('handled_by', $this->handled_by, true);
        $criteria->compare('complete_date', $this->complete_date);
        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}